(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.currency.deleteVerifyController', deleteVerifyController);
	deleteVerifyController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'currencyService' ];
	function deleteVerifyController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, currencyService) {
		var ctrl = this, currencyInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.currency = {};
		ctrl.init = function() {
			var params = {
					ccy : currencyInfo.ccy
			};
			authService.deleteVerify($scope.$stateParams.params).then(function(response) {
				ctrl.currency = response.data.model || {};
				ctrl.currency.taskId = response.data.task.taskId;
			});
		};

		ctrl.deleteVerifyApprove = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else {
				return authService.deleteVerifyApprove(ctrl.currency).then(approveSuccessful, saveFailed);
			}
		};

		ctrl.deleteVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {
					'ok' : function(comments) {
						ctrl.currency.comments = comments;
						authService.deleteVerifyReject(ctrl.currency).then(rejectSuccessful, submitFailed);
					}
				}
			});
		};

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedSubCommpanyList = selectedRows;
		}

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.currency.list',
					'state' : 'cda.currency.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.currency.list',
					'state' : 'cda.currency.list',
					'params' : {}
				}]
			});
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);